package ynu.edu.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import ynu.edu.po.User;
import ynu.edu.service.IUserService;
import ynu.edu.utils.JwtUtils;

import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/user")
@Tag(name = "用户服务")
public class UserController {
    private static final Logger log = LoggerFactory.getLogger(UserController.class);
    @Resource
    private IUserService userService;

    @PostMapping("/getUserByIdByPass")
    @Operation(summary = "查询用户信息",
            description = "根据用户编号与密码查询用户信息")
    @Parameters({
            @Parameter(name = "userId", description = "用户编号"),
            @Parameter(name = "password", description = "密码")
    })
    public User getUserByIdByPass(
            @RequestParam String userId,
            @RequestParam String password) {
        User user = User.builder()
                .userId(userId)
                .password(password)
                .build();
        return userService.getUserByIdByPass(user);
    }

    @RequestMapping("/login")
    public String login(String userId) {
        Map<String,Object> claim = new HashMap<>();
        claim.put("userId", userId);
        log.info(JwtUtils.genToken(claim));
        return JwtUtils.genToken(claim);
    }


    @PostMapping("/getUserById")
    @Operation(summary = "查询用户表信息",
            description = "根据用户编号查询用户表是否有该用户")
    @Parameter(name = "userId", description = "用户编号")
    public boolean getUserById(@RequestParam String userId) {
        return userService.getUserById(userId);
    }

    @PostMapping("/saveUser")
    @Operation(summary = "添加/保存用户信息",
            description = "向用户表中添加一条记录。" +
                    "需要User用户JSON实体," +
                    "实体需包含userId、password、userName、userSex参数")
    public boolean saveUser(@RequestBody User user) {
        return userService.saveUser(user);
    }
}
